<!-- Toast.vue -->
<template>
  <transition name="fade">
    <div v-if="visible" :class="type" class="toast">
      <span v-if="icon" class="icon">{{ icon }}</span>
      <span class="message">{{ message }}</span>
    </div>
  </transition>
</template>

<script lang="ts" setup>
import {onMounted, ref} from 'vue';

const props = defineProps<{
  message: string;
  type: 'success' | 'warning' | 'error' | 'info';
  duration?: number;
}>();

const visible = ref(false);
const iconMap: Record<string, string> = {
  success: '✅',
  warning: '⚠️',
  error: '❌',
  info: 'ℹ️',
};

const icon = iconMap[props.type];

// 显示 Toast
const show = () => {
  visible.value = true;
  setTimeout(() => {
    visible.value = false;
  }, props.duration || 3000);
};

onMounted(() => {
  show();
});
</script>

<style scoped>
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 20px;
  border-radius: 4px;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 9999;
}

.success {
  background-color: #4caf50;
}

.warning {
  background-color: #ff9800;
}

.error {
  background-color: #f44336;
}

.info {
  background-color: #2196f3;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>